/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes.sprites;

import com.android1.amarena2d.animation.Animation;
import com.android1.amarena2d.commons.BlendFunction;
import com.android1.amarena2d.engine.GL;
import com.android1.amarena2d.engine.Resource;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.nodes.behavior.delegates.AnimatedActionTarget;
import com.android1.amarena2d.nodes.behavior.delegates.AnimationDelegate;
import com.android1.amarena2d.nodes.behavior.delegates.SetMeshAnimationDelegate;
import com.android1.amarena2d.texture.ManagedTexture;
import com.android1.amarena2d.texture.TextureFrame;
import com.android1.amarena2d.texture.TexturedMesh;

/**
 * Has a shared TexturedMesh
 */
public class SharedSprite extends BaseSprite implements AnimatedActionTarget.SetMesh, Resource.Consumer {

    protected AnimationDelegate animationDelegate;

    protected BlendFunction blendFunction = BlendFunction.DEFAULT;

    protected TexturedMesh texturedMesh;

    public SharedSprite(TexturedMesh sharedTexturedMesh) {
        this(sharedTexturedMesh, 0, 0);
    }

    public SharedSprite(TexturedMesh sharedTexturedMesh, float x, float y) {
        super(x, y);
        this.texturedMesh = sharedTexturedMesh;
    }

    public SharedSprite(TextureFrame textureFrame) {
        this(textureFrame, 0, 0);
    }

    public SharedSprite(TextureFrame textureFrame, float x, float y) {
        this(textureFrame.getTexturedMesh(), x, y);
    }

    public SharedSprite(ManagedTexture managedTexture) {
        this(managedTexture, 0, 0);
    }


    public SharedSprite(ManagedTexture managedTexture, float x, float y) {
        this(managedTexture.getSharedTexturedMesh(), x, y);
    }

    public SharedSprite(String texturePath) {
        this(texturePath, 0, 0);
    }

    public SharedSprite(String texturePath, float x, float y) {
        this(engine.getTextureManager().getTexture(texturePath), x, y);
    }


    public SharedSprite(Animation animation) {
        this(animation, 0, 0);
    }

    public SharedSprite(Animation animation, float x, float y) {
        super(x, y);
        this.animation().applyFrame(animation.getFrame(0));
        this.animation().setDefaultAnimation(animation);
    }


    @Override
    public void init() {

        if (!init) {
            texturedMesh.obtainRef(this);
            texturedMesh.init();
            update();
            super.init();
        }

    }

    @Override
    public void dispose() {
        if (init) {
            this.texturedMesh.releaseRef(this);
            this.texturedMesh.dispose();
            super.dispose();
        }
    }

    public void setTexturedMesh(TexturedMesh textureMesh) {
        if (this.texturedMesh != null && !textureMesh.equalResource(this.texturedMesh)) {
            this.texturedMesh.releaseRef(this);
        }
        this.texturedMesh = textureMesh;
        if (init) {
            this.texturedMesh.obtainRef(this);
            this.texturedMesh.init();
            update();
        }
    }

    private void update() {
        size.set(texturedMesh.getWidth(), texturedMesh.getHeight());
    }


    @Override
    public void render() {
        if (!init || !active || !visible)
            return;

        if (Scene.CULLING && !this.disableCulling && !this.isVisibleOnScreen())
            return;

        GL.pushMatrix();

        GL.setColor(this.color);

        GL.translate(getX(), getY());

        if (transformDelegate != null) {
            transformDelegate.apply();
        }
        GL.blendFunc(blendFunction);
        texturedMesh.render();

        GL.popMatrix();

    }


    public AnimationDelegate animation() {
        if (animationDelegate == null)
            animationDelegate = new SetMeshAnimationDelegate(this);
        return animationDelegate;
    }

    public BlendFunction getBlendFunction() {
        return blendFunction;
    }

    public void setBlendFunction(BlendFunction blendFunction) {
        this.blendFunction = blendFunction;
    }

    @Override
    public TextureFrame getTextureFrame() {
        return texturedMesh.getTextureFrame();
    }

    @Override
    public void setTextureFrame(TextureFrame textureFrame) {
        setTexturedMesh(textureFrame.getTexturedMesh());
    }

    @Override
    public void setSize(float width, float height) {
        throw new UnsupportedOperationException("can not change size of a shared mesh.");
    }

    @Override
    public void setWidth(float width) {
        throw new UnsupportedOperationException("can not change size of a shared mesh.");
    }

    @Override
    public void setHeight(float height) {
        throw new UnsupportedOperationException("can not change size of a shared mesh.");
    }
}
